package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;


    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {

        Page<Address> addresses;
        PageHelper.startPage(currentPage, pageSize);
        addresses=   addressDao.selectByCondition(queryString);


        long total = addresses.getTotal();
        List<Address> rows = addresses.getResult();

        return new PageResult(total, rows);
    }

    @Override
    public void deleteById(Integer id) {
        if (id < 0 || id == null) {
            return;
        }
        //删中间表再删主表
        addressDao.deleteCenter(id);
        addressDao.deleteById(id);

    }


    @Override
    public void edit(Address address) {

        addressDao.edit(address);
    }


    @Override
    public void add(Address address) {
        //要进行数据合法性校验
        addressDao.add(address);
    }


    @Override
    public Address findById(Integer id){
        return   addressDao.findById(id);
    }

    @Override
    public List<Address> findAllmap() {
        List<Address> list = addressDao.findAllmap();
        for (Address address : list) {
            double lat = address.getLat();
            double longitude = address.getLongitude();
            address.setLl(longitude+ "," +lat);
            System.out.println(address.getLl());
        }
        return list;
    }

    @Override
    public String findAllAddress() {
        return addressDao.findAllAddress();
    }


}
